Task 1
#create GEOID column in bmps from subset GeographyName
bmps$GEOID = stringr::str_sub(bmps$GeographyName, 1, 5)
#sum cost of bmps per geoid
tot.cost <- bmps %>% filter(., Cost > 0) %>% #filter out values of 0
group_by(GEOID) %>% #GEOID to get a single value to join to counties later
summarise(., TotalBMPCosts = as.numeric(format(round(sum(Cost),2), nsmall=2))) #trims costs to 2 decimal places to match currency
#join tot.cost to counties on GEOID, drop counties with no BMP costs
bmp.sf <- left_join(CBWcounties, tot.cost, by = c("GEOID10"="GEOID")) %>%
filter(., TotalBMPCosts > 0) %>%
st_transform(., crs = "EPSG:4326")
#create bins and palette
maxB <- max(bmp.sf$TotalBMPCosts) + 1 #find max value and add 1 for upper bin
bins <- c(0, 0.2*maxB, 0.4*maxB, 0.6*maxB, 0.8*maxB,maxB) #create quantile classes programmaticaly
pal <- colorBin("BuPu", domain = bmp.sf$TotalBMPCosts, bins=bins) #create palette
task1 <- leaflet(bmp.sf) %>%
addTiles() %>%
addPolygons(fillColor = ~pal(TotalBMPCosts),
weight = 2,
opacity = 1,
color = "blue",
fillOpacity = 1,
label = paste0("$", bmp.sf$TotalBMPCosts)) %>%
addLegend(pal = pal,
values = ~TotalBMPCosts,
title = "Total BMP Costs",
position = "bottomright")
task1